package com.yanftch.hdc_tools.insertcode.dialog

import com.intellij.ui.components.JBList
import com.intellij.ui.components.JBScrollPane
import com.yanftch.hdc_tools.insertcode.bean.CodeBean
import java.awt.event.KeyAdapter
import java.awt.event.KeyEvent
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import javax.swing.DefaultListModel
import javax.swing.JFrame
import javax.swing.ListSelectionModel

class InsertCodeFrame(codes: List<CodeBean>) : JFrame("选择要插入的模板名称") {
    private val codeList = JBList<String>()
    private var selectedCode: CodeBean? = null
    private var listener: OnSelectListener? = null

    fun setListener(listener: OnSelectListener) {
        this.listener = listener
    }

    interface OnSelectListener {
        fun onSelect(code: CodeBean)
    }

    init {
        // 设置 JFrame 的基本属性
        defaultCloseOperation = HIDE_ON_CLOSE
        setSize(300, 400)
        setLocationRelativeTo(null) // 窗口居中显示

        // 将 Code 对象的 name 字段填充到 JList 中
        val codeNames = codes.map { it.name }.toTypedArray()
        codeList.model = DefaultListModel<String>().apply {
            codeNames.forEach { addElement(it) }
        }
        codeList.fixedCellHeight = 30
        codeList.selectionMode = ListSelectionModel.SINGLE_SELECTION

        // 添加选择监听器
//        codeList.addListSelectionListener { e ->
//            if (!e.valueIsAdjusting) {  // 确保只在用户停止选择时触发
//                val selectedIndex = codeList.selectedIndex
//                if (selectedIndex >= 0) {
//                    selectedCode = codes[selectedIndex]
//                    println("Selected Code: ${selectedCode?.name}, Value: ${selectedCode?.value}")
//                }
//            }
//        }

        // 处理回车事件
        codeList.addKeyListener(object : KeyAdapter() {
            override fun keyPressed(e: KeyEvent) {
                if (e.keyCode == KeyEvent.VK_ENTER) {
                    val selectedIndex = codeList.selectedIndex
                    if (selectedIndex != -1) {
                        val selectedCode = codes[selectedIndex]
                        select(selectedCode)
                    }
                } else if (e.keyCode == KeyEvent.VK_ESCAPE) {
                    isVisible = false
                }
            }
        })

        // 添加双击事件
        codeList.addMouseListener(object : MouseAdapter() {
            override fun mouseClicked(e: MouseEvent) {
                if (e.clickCount == 2) {
                    val selectedIndex = codeList.selectedIndex
                    if (selectedIndex != -1) {
                        val selectedCode = codes[selectedIndex]
                        select(selectedCode)
                    }
                }
            }
        })


        // 将 JList 放入 JScrollPane 以支持滚动
        val scrollPane = JBScrollPane(codeList)
        add(scrollPane)

        // 添加按钮以关闭窗口
//        val closeButton = JButton("Close")
//        closeButton.addActionListener { dispose() }
//        add(closeButton, "South") // 在底部添加关闭按钮

        isVisible = true // 显示窗口
    }

    private fun select(bean: CodeBean) {
        listener?.onSelect(bean)
        isVisible = false
    }
}